<?php
namespace Admin\Controller;
use Think\Page;

use Org\Net\Http;

use Think\Controller;

use Common\Controller\AuthController;
class PointController extends AuthController {
	public function index(){
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$types = array('Ios','Android');
			$type = $_POST['type'];
			if(in_array_case($type, $types)){
				$point = D($type.'Point');
				$data['name'] = $_POST['name'];
				$data['latitude'] = $_POST['latitude'];
				$data['longitude'] = $_POST['longitude'];
				$data = $point->create($data);
				$result = $point->add($data);
				if($result){
					$json['result'] = true;
				}else{
					$json['result'] = false;
					$json['err'] = '添加失败：添加数据库失败';
				}
			}else{
				$json['result'] = false;
				$json['err'] = '添加失败：未知类型';
			}
			$this->ajaxReturn($json);
		}
	}
	public function query(){
		$geotables = array('ios'=>'105083','android'=>'105084');
		$action = I('action');
		$tags = I("tags");
		$p = I("p");
		if($p != 0){
			$p = $p -1;
		}
		$http = new Http();
		$page_size = 7;
		$url = C('BAIDU_POI_LIST')."?ak=".C('BAIDU_AK')."&geotable_id={$geotables[$action]}&tags={$tags}&page_size={$page_size}&page_index={$p}";
		$back = $http->fsockopenDownload($url);
		$back = json_decode($back);
		$pois = $back->pois;
		$page = new Page($back->total,$page_size);
		$page->setConfig('theme','%UP_PAGE% %LINK_PAGE% %DOWN_PAGE%');
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$this->assign('list',$pois);
		$this->assign('total',$page->totalPages);
		$this->assign('show',$show);
		$this->display($action);
	}
}